<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\AdminMenu;
use App\Services\BaseService;

class MenuController extends Controller
{

    //
    public function create(Request $request)
    {
        $body = $request->post();

        $menu = AdminMenu::query()
                         ->findOrNew($body['id'] ?? 0);

        $menu->parent_id = $body['parent_id'] ?? 0;
        $menu->order = $body['order'];
        $menu->title = $body['title'];
        $menu->icon = $body['icon'];
        $menu->uri = $body['uri'];

        return $menu->save();
    }

    public function index(BaseService $baseService)
    {
        $menu = AdminMenu::query()
                         ->orderBy('order')
                         ->get();

        return $baseService->getTree($menu->toArray());
    }

    public function show(int $id)
    {
        return AdminMenu::find($id);
    }
}
